package com.woniuxy.shop.servelet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.woniuxy.shop.entity.DetailOrder;
import com.woniuxy.shop.entity.Order;
import com.woniuxy.shop.entity.PageBean;
import com.woniuxy.shop.entity.ResponseEntity;
import com.woniuxy.shop.exception.ServiceException;
import com.woniuxy.shop.service.AdminService;
import com.woniuxy.shop.service.DetailOrderService;
import com.woniuxy.shop.service.OrderService;
import com.woniuxy.shop.service.ServiceProxyFactory;

/**
 * Servlet implementation class CateGoryServelet
 */
@WebServlet("/order")
public class OrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	OrderService os = ServiceProxyFactory.getProxy(OrderService.class);
	DetailOrderService dos = new DetailOrderService();
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		request.setCharacterEncoding("utf-8");// 获取参数前指定编码
//		response.setCharacterEncoding("utf-8");
		String opr = request.getParameter("opr");
		if ("list".equals(opr)) {
			doList(request, response);
		} else if("add".equals(opr)) {
			doAdd(request,response);
		}else if("detailOrder".equals(opr)) {
			doDetailOrder(request,response);
		}
	}
	

	private void doDetailOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			int id = Integer.parseInt(request.getParameter("id"));
			List<DetailOrder> dors = dos.getDetailOrder(id);
			Order o  = os.getOrder(id);
			request.setAttribute("order", o);
			request.setAttribute("dors", dors);
		} catch (Exception e) {
			
		}
		request.getRequestDispatcher("order/order_view.jsp").forward(request, response);
		
	}


	private void doAdd(HttpServletRequest request, HttpServletResponse response) {
		Order o = new Order();
		DetailOrder dor = new  DetailOrder();
		
	}


	private void doList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//取值
		String orderno = request.getParameter("orderno");
		String starttime = request.getParameter("starttime");
		String endtime = request.getParameter("endtime");
		String status = request.getParameter("status");
		String spage = request.getParameter("p");
		int page=1;//默认页为第一页
		//将查询条件封装成Order
		Order o = new Order();
		if(orderno!=null&&!orderno.trim().equals("")) {
			o.setOrderno(orderno);
		}
		if(starttime!=null&&!starttime.trim().equals("")) {
			o.setStarttime(starttime);
		}
		if(endtime!=null&&!endtime.trim().equals("")) {
			o.setEndtime(endtime);
		}
		if(status!=null&&!status.trim().equals("")&&!status.equals("不限")) {
			o.setStatus(status);
		}
		if(spage!=null&&!spage.trim().equals("")) {
			page=Integer.parseInt(spage);
		}
		ResponseEntity<PageBean<Order>> rese = new ResponseEntity<>();
		PrintWriter writer = response.getWriter();
		try {
			System.out.println(page);
			PageBean<Order> pb = os.getPageBean(page, o);
			rese.setCode(200);
			rese.setData(pb);
			rese.setMsg("ok");
		} catch (ServiceException e) {
			rese.setCode(500);
			rese.setMsg("error");
		}
		String json = JSON.toJSONString(rese);
		writer.write(json);
		writer.close();
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
	


}
